<!DOCTYPE html>
<html>
<head>

  <title>Native Client</title>

  <script type="text/javascript">
    HelloTutorialModule = null;  // Global application object.
    statusText = 'NO-STATUS';

    // Indicate load success.
    function moduleDidLoad() {
      HelloTutorialModule = document.getElementById('native_client');
      updateStatus('SUCCESS');
    }

    // The 'message' event handler.  This handler is fired when the NaCl module
    // posts a message to the browser by calling PPB_Messaging.PostMessage()
    // (in C) or pp::Instance.PostMessage() (in C++).  This implementation
    // simply displays the content of the message in an alert panel.
    function handleMessage(message_event) {
      if (message_event.data['message'] == 'request_textures') {
        // NaCl module is requesting a set of textures.
        var names = message_event.data['names'];
        for (var i = 0; i < names.length; i++)
          loadTexture(names[i]);
	return;
      }

      console.log(message_event.data);
    }

    // If the page loads before the Native Client module loads, then set the
    // status message indicating that the module is still loading.  Otherwise,
    // do not change the status message.
    function pageDidLoad() {
      if (HelloTutorialModule == null) {
        updateStatus('LOADING...');
      } else {
        // It's possible that the Native Client module onload event fired
        // before the page's onload event.  In this case, the status message
        // will reflect 'SUCCESS', but won't be displayed.  This call will
        // display the current message.
        updateStatus();
      }
    }

    // Set the global status message.  If the element with id 'statusField'
    // exists, then set its HTML to the status message as well.
    // opt_message The message test.  If this is null or undefined, then
    // attempt to set the element with id 'statusField' to the value of
    // |statusText|.
    function updateStatus(opt_message) {
      if (opt_message)
        statusText = opt_message;
      var statusField = document.getElementById('statusField');
      if (statusField) {
        statusField.innerHTML = statusText;
      }


    }

    function loadTexture(name) {
      // Load image from jpg, decompress into canvas.
      var img = new Image();
      img.onload = function() {
        var graph = document.createElement('canvas');
        graph.width = img.width;
        graph.height = img.height;
        var context = graph.getContext('2d');
        context.drawImage(img, 0, 0);
        var imageData = context.getImageData(0, 0, img.width, img.height);
        // Send NaCl module the raw image data obtained from canvas.
        document.getElementById('native_client')
		.postMessage({
			'message' : 'texture',
                        'name' : name,
                        'width' : img.width,
                        'height' : img.height,
                        'data' : imageData.data.buffer});
      }
      img.src = name;
    }

  </script>
</head>
<body onload="pageDidLoad()">

  <h1>NaCl App</h1>
  <p>
    <div id="listener">
      <script type="text/javascript">
        var listener = document.getElementById('listener');
        listener.addEventListener('load', moduleDidLoad, true);
        listener.addEventListener('message', handleMessage, true);
      </script>

      <embed id="native_client"
             width=512 height=512
             src="NativeClient.nmf"
             type="application/x-pnacl" />
    </div>
  </p>

  <h2>Status <code id="statusField">NO-STATUS</code></h2>
</body>
</html>
